#include "include.h"
#include "typedef.h"
#include "config.h"
#include "time.h"
#include "control.h"

extern intime_t intime[7];

#define Mon  intime[0]
#define Tue  intime[1]
#define Wed  intime[2]
#define Thur intime[3]
#define Fri  intime[4]
#define Sat  intime[5]
#define Sun  intime[6]



void intime_init(void){
	Mon.onTime = 0; Mon.offTime = 0;Mon.setTemp = 26;
	Tue.onTime = 0; Tue.offTime = 0;Tue.setTemp = 26;
	Wed.onTime = 0; Wed.offTime = 0;Wed.setTemp = 26;
	Thur.onTime = 0; Thur.offTime = 0;Thur.setTemp = 26;
	Fri.onTime = 0; Fri.offTime = 0;Fri.setTemp = 26;
	Sat.onTime = 0; Sat.offTime = 0;Sat.setTemp = 26;
	Sun.onTime = 0; Sun.offTime = 0;Sun.setTemp = 26;
}

void set_onTime(uint8_t time){
	if(__CF_INTIME_WEEK == 1)      Mon.onTime = time;
	else if(__CF_INTIME_WEEK == 2) Tue.onTime = time;
	else if(__CF_INTIME_WEEK == 3) Wed.onTime = time;
	else if(__CF_INTIME_WEEK == 4) Thur.onTime = time;
	else if(__CF_INTIME_WEEK == 5) Fri.onTime = time;
	else if(__CF_INTIME_WEEK == 6) Sat.onTime = time;
	else if(__CF_INTIME_WEEK == 7) Sun.onTime = time;
}

void set_offTime(uint8_t time){
	if(__CF_INTIME_WEEK == 1)      Mon.offTime = time;
	else if(__CF_INTIME_WEEK == 2) Tue.offTime = time;
	else if(__CF_INTIME_WEEK == 3) Wed.offTime = time;
	else if(__CF_INTIME_WEEK == 4) Thur.offTime = time;
	else if(__CF_INTIME_WEEK == 5) Fri.offTime = time;
	else if(__CF_INTIME_WEEK == 6) Sat.offTime = time;
	else if(__CF_INTIME_WEEK == 7) Sun.offTime = time;
}

void set_Temp(uint8_t temp){
	if(__CF_INTIME_WEEK == 1)      Mon.setTemp = temp;
	else if(__CF_INTIME_WEEK == 2) Tue.setTemp = temp;
	else if(__CF_INTIME_WEEK == 3) Wed.setTemp = temp;
	else if(__CF_INTIME_WEEK == 4) Thur.setTemp = temp;
	else if(__CF_INTIME_WEEK == 5) Fri.setTemp = temp;
	else if(__CF_INTIME_WEEK == 6) Sat.setTemp = temp;
	else if(__CF_INTIME_WEEK == 7) Sun.setTemp = temp;
}

 
void time_check(uint8_t week){
	uint8_t  time_index;
	time_index = RTC_HOUR * 2 + RTC_MIN / 30;
	if(RTC_MIN % 30 == 0 && RTC_SEC == 0){
		if(intime[week - 1].onTime != 0 && intime[week - 1].offTime != 0){
			if(intime[week - 1].offTime == time_index){
					__CF_POWER = 0;
					return;
			}
			if(intime[week - 1].onTime == time_index){
					__CF_POWER = 1;
					__CF_TEMP_FUNC = intime[week - 1].setTemp;
					clr_model();
					__CF_TEMP = 1;
			}
		}
	}
}

void time_func(void){
	if(RTC_WEEK == 1) time_check(1);
	else if(RTC_WEEK == 2) time_check(2);
	else if(RTC_WEEK == 3) time_check(3);
	else if(RTC_WEEK == 4) time_check(4);
	else if(RTC_WEEK == 5) time_check(5);
	else if(RTC_WEEK == 6) time_check(6);
	else if(RTC_WEEK == 7) time_check(7);
}
